import numpy as np
import librosa
import librosa.display
import matplotlib.pyplot as plt
import soundfile as sf
if __name__ == "__main__":

    # # 去除头尾的静音
    # y,fs = librosa.load('test1.wav',sr = 16000)
    # print(len(y))
    # yt, index = librosa.effects.trim(y,top_db=30)
    # print(index)
    # fig,axs = plt.subplots(nrows=2,ncols=1)

    # librosa.display.waveshow(y, sr=fs, ax=axs[0])
    # axs[0].vlines(index[0]/fs,-0.5,0.5,colors='r')
    # axs[0].vlines(index[1]/fs,-0.5,0.5,colors='r')
    # librosa.display.waveshow(yt, sr=fs, ax=axs[1])

   

    # sf.write('test_trim.wav',yt,fs)
    # plt.show()

    # # 分割
    # y,fs = librosa.load('test1.wav',sr = 16000)
    # intervals = librosa.effects.split(y, top_db=20)
    # print(intervals)
    # y_remix = librosa.effects.remix(y,intervals)
    # fig,axs = plt.subplots(nrows=2,ncols=1,sharex=True, sharey=True)
    # librosa.display.waveshow(y, sr=fs,ax = axs[0])
    # librosa.display.waveshow(y_remix, sr=fs,ax = axs[1],offset=intervals[0][0]/fs)
    
    # for interval in intervals:
    #     axs[0].vlines(interval[0]/fs,-0.5,0.5,colors='r')
    #     axs[0].vlines(interval[1]/fs,-0.5,0.5,colors='r')
    # plt.show()
    # sf.write('test_split.wav',y_remix,fs)
    # print(len(y_remix))

    
    # 频域表示stft
    # y,fs = librosa.load('test1.wav',sr = 16000)
    # frame_t =25  # 25ms 帧长
    # hop_length_t = 10 # 10ms 步近

    # win_length = int(frame_t*fs/1000)
    # hop_length = int(hop_length_t*fs/1000)
    # n_fft = int(2**np.ceil(np.log2(win_length)))

    # # n_fft =512
    # # win_length =512
    # # hop_length = 256

    # S = np.abs(librosa.stft(y, n_fft=n_fft, hop_length=hop_length, win_length=win_length))

    # fig = plt.figure()
    # plt.imshow(S,origin='lower',cmap='hot')

    # S = librosa.amplitude_to_db(S,ref=np.max)
    # D,N = S.shape
    # range_D = np.arange(0,D,20)
    # range_N = np.arange(0,N,20)
    # range_t = range_N*(hop_length/fs)
    # range_f = range_D*(fs/n_fft/1000)

    # # 自己写显示程序
    # fig = plt.figure()
    # plt.xticks(range_N,range_t)
    # plt.yticks(range_D,range_f)
    # print(S.shape)
    # plt.imshow(S,origin='lower',cmap='hot')
    # plt.colorbar()
    # # plt.show()

    # # 调用内置的显示程序
    # fig = plt.figure()
    # librosa.display.specshow(S, y_axis='linear', x_axis='time',hop_length=hop_length, sr=fs)
    # plt.colorbar()
    # plt.show()

    # 预加重 pre-emphasis

    n_fft =512
    win_length =512
    hop_length = 160

    y,fs = librosa.load('test_split.wav',sr=None)
    y_filt = librosa.effects.preemphasis(y)
    sf.write("test_trim_pre.wav",y_filt,fs)
    S = librosa.stft(y,n_fft=n_fft,hop_length=hop_length,win_length=win_length)
    S = librosa.amplitude_to_db(np.abs(S))

    S_preemp = librosa.stft(y_filt,n_fft=512,hop_length=hop_length,win_length=win_length)
    S_preemp = librosa.amplitude_to_db(np.abs(S_preemp))

    fig,axs = plt.subplots(2,1,sharex=True,sharey=True)

    librosa.display.specshow(S,sr=fs,hop_length=hop_length,y_axis='linear',x_axis='time', ax = axs[0])
    axs[0].set(title='Original signal')
    
    img = librosa.display.specshow(S_preemp,sr=fs,hop_length=hop_length,y_axis='linear',x_axis='time',ax = axs[1])
    axs[1].set(title='pre-emphasis signal')
    fig.colorbar(img, ax=axs, format="%+2.f dB")
    plt.show()

    # fbank 特征
    # 滤波器组
    # y,fs = librosa.load('test1.wav',sr=16000)
    # win_length =512
    # hop_length = 160
    # n_fft = 512
    # n_mels= 40
    # melfb = librosa.filters.mel(sr=fs, n_fft=n_fft,n_mels = n_mels,htk=True)
    # print(melfb.shape)
    # x = np.arange(melfb.shape[1])*fs/n_fft
    # fig = plt.figure()
    # plt.plot(x,melfb.T)
    # plt.show()
    # fig = plt.figure()
    # fbank = librosa.feature.melspectrogram(y,
    #                                        sr = fs, 
    #                                        n_fft = n_fft,
    #                                        win_length = win_length,
    #                                        hop_length = hop_length,
    #                                        n_mels = n_mels)
    # print(fbank.shape)
    # fbank_db  = librosa.power_to_db(fbank, ref=np.max)
    # img = librosa.display.specshow(fbank_db, x_axis='time', y_axis='mel', sr=fs,fmax=fs/2,)
    # fig.colorbar(img,format='%+2.0f dB')
    # plt.title('Mel-frequency spectrogram')
    
    # plt.show()

    # # MFCC 特征
    # # 直接计算
    # y,fs = librosa.load('test1.wav',sr=16000)
    # win_length =512
    # hop_length = 160
    # n_fft = 512
    # n_mels = 128
    # n_mfcc = 20
    # mfcc1 = librosa.feature.mfcc(y, 
    #                              sr=fs, 
    #                              n_mfcc=n_mfcc, 
    #                              win_length = win_length,
    #                              hop_length =hop_length,
    #                              n_fft = n_fft,
    #                              n_mels = n_mels)
    # print(mfcc1.shape)

    # # 也可以提前计算好 log-power fbank特征来进行 mfcc的计算
    # fbank = librosa.feature.melspectrogram(y,
    #                                        sr = fs, 
    #                                        n_fft = n_fft,
    #                                        win_length = win_length,
    #                                        hop_length= hop_length,
    #                                        n_mels = n_mels)
    
    # print(fbank.shape)
    # fbank_db  = librosa.power_to_db(fbank, ref=np.max)

    # mfcc2 = librosa.feature.mfcc(S = fbank_db,n_mfcc=20,sr = fs)
    # print(mfcc2.shape)
    # fig = plt.figure()
    # img = librosa.display.specshow(mfcc1, x_axis='time')
    # fig.colorbar(img)
    # plt.title("MFCC")
    # plt.show()

    # # # MFCC 不同DCT方式
    # y,fs = librosa.load('test1.wav',sr=16000)
    # win_length =512
    # hop_length = 160
    # n_fft = 512
    # n_mels = 128
    # n_mfcc = 20
    # mfcc1 = librosa.feature.mfcc(y, 
    #                              sr=fs, 
    #                              n_mfcc=n_mfcc, 
    #                              win_length = win_length,
    #                              hop_length =hop_length,
    #                              n_fft = n_fft,
    #                              n_mels = n_mels,
    #                              dct_type=1
    #                              )
    
    # mfcc2 = librosa.feature.mfcc(y, 
    #                              sr=fs, 
    #                              n_mfcc=n_mfcc, 
    #                              win_length = win_length,
    #                              hop_length =hop_length,
    #                              n_fft = n_fft,
    #                              n_mels = n_mels,
    #                              dct_type=2
    #                              )
    
    # mfcc3 = librosa.feature.mfcc(y, 
    #                              sr=fs, 
    #                              n_mfcc=n_mfcc, 
    #                              win_length = win_length,
    #                              hop_length =hop_length,
    #                              n_fft = n_fft,
    #                              n_mels = n_mels,
    #                              dct_type=3
    #                              )
    # fig, axs = plt.subplots(nrows=3, sharex=True, sharey=True)
    # img1 = librosa.display.specshow(mfcc1, x_axis='time',ax = axs[0])
    # axs[0].set_title("DCT type 1")
    # fig.colorbar(img1,ax=axs[0])
    
    # img2 = librosa.display.specshow(mfcc2, x_axis='time',ax=axs[1])
    # axs[1].set_title("DCT type 2")
    # fig.colorbar(img2,ax=axs[1])

    # img3 = librosa.display.specshow(mfcc3, x_axis='time',ax = axs[2])
    # axs[2].set_title("DCT type 3")
    # fig.colorbar(img3,ax=axs[2])
    
    # plt.show()

    # # 特征中增加差分量
    # y,fs = librosa.load('test1.wav',sr=16000)
    # win_length =512
    # hop_length = 160
    # n_fft = 512
    # n_mels = 128
    # n_mfcc = 20
    # mfcc = librosa.feature.mfcc(y, 
    #                              sr=fs, 
    #                              n_mfcc=n_mfcc, 
    #                              win_length = win_length,
    #                              hop_length =hop_length,
    #                              n_fft = n_fft,
    #                              n_mels = n_mels,
    #                              dct_type=1
    #                              )
    # # 一阶差分
    # mfcc_deta =  librosa.feature.delta(mfcc)
    # # 二阶差分
    # mfcc_deta2 = librosa.feature.delta(mfcc,order=2)

    # # 特征拼接
    # mfcc_d1_d2 = np.concatenate([mfcc,mfcc_deta,mfcc_deta2],axis=0)
    # fig = plt.figure()
    # img = librosa.display.specshow(mfcc_d1_d2, x_axis='time',hop_length=hop_length, sr=fs)
    # fig.colorbar(img)
    # plt.show()




















